<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Cart extends CI_Controller {
    function __construct() {
        parent::__construct();

        // Cart Lib load
        $this->load->library('cart');
        // Url Helper load
        $this->load->helper('url');
        // Form Helper load
        $this->load->helper('form');
 		$this->load->database();
		$this->seg_exp = segment_explode($this->uri->uri_string());
    }

    function index() {
    	
    	$this->load->model("adm/commodity_m");
    	$info = array();
    	foreach($this->cart->contents() as $items){
    		$commodityInfo = $this->commodity_m->get_commodity_info_c($items['id']);
    		if($commodityInfo == null){
    		}else{
    			$info[] = array (
    				'sku_code' => $items['id'],
    				'qty' => $items['qty'],
    				'sku_info' => $commodityInfo,
    				'cart_info'	=> $items
    			);
    		}
    	}
    	
    	
    	$cart_item = $this->_create_cart_items($info);
    	
    	//echo "<pre>";var_dump($cart_item);echo "</pre>";
    	 
    	$customer_info = $this->_create_customer_info($info);

    	$data = array();
    	$data['data'] = $cart_item;
    	$data['customer_info'] = $customer_info;
    	
    	//echo "<pre>";var_dump($cart_item);echo "</pre>";
    	$this->load->view('common/header');
     	//$this->load->view('order/cart_v', array('data'=>$cart_item));
     	$this->load->view('order/cart_v', $data);
    	$this->load->view('common/footer');
    	   
    }
    
    function _create_customer_info(){
    	$customer_info = array();
    
    	if($this->session->userdata('is_login')){
			//TODO::set -> customer,address 
    	}else{
    		if($this->session->userdata('guest_ok')){
    			//TODO::set -> customer,address
    		}else{
    			
    		}
    	}
    	$customer_info  = array();
    	$customer_info['aa'] = "aa";
    	$customer_info['is_guest'] = TRUE;
    	return $customer_info;
    }
    
    function _create_cart_items($carts = array()){
    	$cart_commoditys = array() ;
    	$total_price = 0;
    	$total_count = 0;
    	
    	foreach($carts as $item){
    		
    		$header = $item['sku_info']['header'];
    		$detail = $item['sku_info']['detail'];
    		//echo "<pre>";var_dump($detail);echo "</pre>";
    		$commodity = array();
    		$commodity['commodity_code'] = $detail->commodity_code;
    		$commodity['sku_code'] = $detail->sku_code;
    		$commodity['qty'] = $item['qty'];
    		$commodity['commodity_name'] = $header->commodity_name;
    		$commodity['jan_code'] = $detail->jan_code;
    		$commodity['standard_detail1_code'] = $detail->standard_detail1_code;
    		$commodity['standard_detail2_code'] = $detail->standard_detail2_code;
    		$commodity['unit_price'] = $detail->unit_price;
    		$commodity['sub_total'] = $detail->unit_price * $item['qty'];
    		$commodity['rowid'] = $item['cart_info']['rowid'];
 			$cart_commoditys['cart_item'][] = $commodity;
     		$total_price += $detail->unit_price * $item['qty'];
    		$total_count+=$item['qty'];
    	}
     	$cart_commoditys['total_price'] = $total_price;
     	$cart_commoditys['total_count'] = $total_count;
    	
    	
    	return $cart_commoditys;
    }

    // Cart Add
    function add() {
        $cart_data = array(
            'commodity_code' => $this->input->post('commodity_code'),
            'id'        	 => $this->input->post('id'),
            'qty'       	 => $this->input->post('qty'),
            'price'     	 => $this->input->post('price'),
            'name'      	 => $this->input->post('name'),
            'options'   	 => array(
            'Size' 			 => $this->input->post('size'),
            'Color' 		 => $this->input->post('color')
            )
        );

        $this->cart->insert($cart_data);
        $this->_go_cart();
    }

    // Cart Update
    function update() {
        $cart_data = array();
        $qty = $this->input->post('qty');
        $rowid = $this->input->post('rowid');
        $del = $this->input->post('del');

        for($i=0; $i < count($del); $i++) {
            $qty[$del[$i]] = 0;
        }

        for($i=0; $i < count($rowid); $i++) {
            $cart_data[$i] = array('qty' => $qty[$i], 'rowid' => $rowid[$i]);
        }
        $this->cart->update($cart_data);
        $this->_go_cart();
    }

    // Cart Destory
    function destroy() {
        $this->cart->destroy();
        $this->_go_cart();
    }

    // Display Cart
    function _go_cart() {
        redirect('cart');
    }
    
    function add_cart_aj(){
    	$this->load->library('cart');
    	$cart_data = array(
    			'id'        	 => $this->input->post('commodity_code'),
    			'qty'       	 => $this->input->post('qty'),
    			'price'     	 => $this->input->post('price'),
    			'name'      	 => $this->input->post('commodity_name')
    	);
    		
    	$rtn = $this->cart->upsert($cart_data);
    	$result = array(
    			'result'=> $rtn
    			,'itmes'=> $this->cart->total_items()
    	);
    	echo json_encode($result);
    }
    
    function add_cart(){
    	$this->load->library('cart');
    	$cart_data = array(
    			'id'        	 => $this->input->post('commodity_code'),
    			'qty'       	 => $this->input->post('qty'),
    			'price'     	 => $this->input->post('price'),
    			'name'      	 => $this->input->post('commodity_name')
    	);
    
    	$rtn = $this->cart->upsert($cart_data);
    	$result = array(
    			'result'=> $rtn
    			,'itmes'=> $this->cart->total_items()
    	);
    	redirect('cart');
    }
}